package com.cloudoer.blog.service.impl;

import com.cloudoer.blog.base.BaseService;
import com.cloudoer.blog.exception.ServiceException;
import com.cloudoer.blog.model.Article;
import com.cloudoer.blog.model.ArticleTag;
import com.cloudoer.blog.model.info.ArticleInfo;
import com.cloudoer.blog.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service(value = "articleService")
public class ArticleServiceImpl extends BaseService implements ArticleService {
    @Override
    public ArticleInfo getById(String id) {
        ArticleInfo articleInfo = articleDao.selectById(id);
        Article article = new Article();
        article.setId(articleInfo.getId());
        article.setEndorse(articleInfo.getEndorse());
        articleDao.updateByPrimaryKeySelective(article);
        return articleInfo;
    }

    @Override
    public List<ArticleInfo> getByPage(int pstart, int psize) {
        return articleDao.selectByPage(pstart, psize);
    }

    @Override
    public List<ArticleInfo> getByPagePub(int pstart, int psize) {
        return articleDao.selectByPagePub(pstart, psize, "已发布");
    }

    @Override
    public List<ArticleInfo> getByRan() {
        return articleDao.selectByRan(0, 10, "已发布");
    }

    @Override
    public List<ArticleInfo> getByEnd() {
        return articleDao.selectByRan(0, 10, "已发布");
    }

    @Override
    public ArticleInfo add(Article article) {
        article.setId(UUID.randomUUID().toString().replace("-", ""));
        article.setPostDate(new Date());
        article.setRanking(0);
        article.setState("未发布");
        articleDao.insertSelective(article);
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setId(article.getId());
        articleInfo.setPostDate(article.getPostDate());
        articleInfo.setRanking(article.getRanking());
        articleInfo.setState(article.getState());
        return articleInfo;
    }

    @Override
    public int update(Article article, String tags) {
        if (StringUtils.isNotBlank(tags)){
            for (String temp : tags.split(",")){
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getId());
                articleTag.setTagId(temp);
                articleTag.setId(UUID.randomUUID().toString().replace("-", ""));
                articleTagDao.insert(articleTag);
            }
        }
        return articleDao.updateByPrimaryKeySelective(article);
    }

    @Override
    public int delete(String id) {
        return articleDao.deleteByPrimaryKey(id);
    }

    @Override
    public int getCount() {
        return articleDao.selectCount();
    }

    @Override
    public void publish(String id) {
        Article article = articleDao.selectById(id);
        if (article == null){
            throw new ServiceException("文章不存在");
        }
        String state = "未发布".equals(article.getState()) ? "已发布" : "未发布";
        Article article1 = new Article();
        article1.setId(article.getId());
        article1.setState(state);
        articleDao.updateByPrimaryKeySelective(article1);
    }

    @Override
    public int getCountByPub() {
        return articleDao.selectCountByPub("已发布");
    }
}
